package com.kxf.androidtestdemo.activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.adapter.MyFragmentPageAdapter;
import com.kxf.androidtestdemo.fragments.BlankFragment;

import java.util.ArrayList;
import java.util.Arrays;

public class ViewPagerActivity extends BaseActivity {

    private ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        initView();
    }

    private void initView() {
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        ArrayList<Fragment> fragments = new ArrayList<>(Arrays.asList(new Fragment[]{
                BlankFragment.newInstance("页面1", ""),
                BlankFragment.newInstance("页面2", ""),
                BlankFragment.newInstance("页面3", ""),
                BlankFragment.newInstance("页面4", ""),
                BlankFragment.newInstance("页面5", ""),
                BlankFragment.newInstance("页面6", "")
        }));
        viewpager.setAdapter(new MyFragmentPageAdapter(getSupportFragmentManager(), fragments.size(), fragments));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpager.setCurrentItem(0);
    }
}